#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct node
{
	char data;
	node *lchild,*rchild;
}BiNode,*BiTree;
BiTree CreatBiTree()
{
	BiTree T;
	char ch;
	cin>>ch;
	if(ch=='#')
		T=NULL;
	else
	{
		T=(BiTree)malloc(sizeof(BiNode));
		T->data=ch;
		T->lchild=CreatBiTree();
		T->rchild=CreatBiTree();
	}
	return T;
}
int count=0;
void GetNumber(BiTree T,char ch)
{
	if(T!=NULL)
	{
		if(T->data==ch)
		{
			if(T->lchild!=NULL)
				count++;
			if(T->rchild!=NULL)
				count++;
		}
		else
		{
			GetNumber(T->lchild,ch);
			GetNumber(T->rchild,ch);
		}
	}
}
int main()
{
	BiTree T=CreatBiTree();
	char ch;
	cin>>ch;
	GetNumber(T,ch);
	cout<<count<<endl;
	return 0;
}